package com.villaday.idf.activity;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;

import com.google.android.material.tabs.TabLayout;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.fragment.FodderFragment;
import com.villaday.idf.fragment.GoodsFragment;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.TabLayoutUtils;

import butterknife.BindView;
import butterknife.OnClick;

/*
* 商品  素材
*
* */
public class GoodsAndFodderActivity extends BaseActivity implements GoodsFragment.ChangePage {

    @BindView(R.id.toolbar_iconBack)
    ImageButton toolbarIconBack;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.toolbar_iconNext)
    ImageButton toolbarIconNext;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.toolbar_icon)
    Toolbar toolbarIcon;
    private GoodsFragment instance;


    @Override
    protected int getViewResId() {
        return R.layout.activity_goods_and_fodder;
    }

    @Override
    protected void init() {
        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(this);
        String roleType = roleInfo.getMember().getType();
        int type = getIntent().getIntExtra("type", 0);

        final String persellId = getIntent().getStringExtra("persellId");
        final String houseId = getIntent().getStringExtra("houseId");
//右边图标
        toolbarIconNext.setVisibility(View.VISIBLE);
        toolbarIconNext.setImageResource(R.drawable.detailspage_share);
        final String[] titles;
       if ("0".equals(roleType)){
           titles = new String[]{"详情"};
           tab.setSelectedTabIndicatorHeight(0);
       }else {
           titles = new String[]{"详情", "素材"};
       }


        instance = GoodsFragment.instance(houseId,persellId);
        instance.setChangePage(this);
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return position == 0 ? instance : FodderFragment.instance(persellId,houseId,getIntent().getLongExtra("brokerAge",0));
            }

            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        tab.setupWithViewPager(vp);
        vp.setCurrentItem(type);
        TabLayoutUtils.reflex(tab, 5);


    }


    @OnClick({R.id.toolbar_iconBack, R.id.toolbar_iconNext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_iconBack: {//返回
                onBackPressed();
            }
            break;
            case R.id.toolbar_iconNext: {//右边图标
                if (instance!=null){
                    instance.shareXCX();
                }


            }
            break;
        }
    }


    @Override
    public void changePage() {
        vp.setCurrentItem(1);
    }

    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }
}
